#ifndef GCode_H_
#define GCode_H_
#include <QtOpenGL/QtOpenGL>
#include <string>
#include <vector>
#include <QVector3D>

struct GCodeLine
{
	std::string originalLine;  // the original line from the file
    std::string clearedLine;    // the cleared line, no comments, no needed whitespaces, etc. 
    std::string command;
    bool interprete;   // if this line is interpreteable (not just an empty or comment line)
//	vector<GCodeParameter> parameters;
    bool hasE;
    bool hasXYZ;
    bool visible;
    int layer;

    float x;
    float y;
    float z;
    float e;
    float f;
};


class GCode
{
public:
    GCode();
    ~GCode();
    int   open(std::string fileName);
    void  clear();
    void  draw(bool linesOnly, bool showMotion);
    void  drawPartTube(int segments);
    int   addLine(std::string line);
    void  parseLine(GCodeLine &gcodeLine, char command, std::string value);
    std::vector<GCodeLine>& getCodeLines() ;
    void  refreshMinMax();
	float getMinX();
	float getMinY();
	float getMinZ();
	float getMaxX();
	float getMaxY();
	float getMaxZ();
    void setDrawSegments(GLsizei segs);
    int  getGCodeCount() {
        return codeLines.size();
    }
    void setGCodeLayers(int layers);
    void drawTube();
    bool fullydraw;
    bool  isOpen() const;
protected:
	
private:
    GLsizei segments;
	std::vector<GCodeLine> codeLines;
	void replace(std::string& replace, std::string from, std::string to);
	void explode(std::vector<std::string> &res, std::string&list, std::string expression);
    void generateTube(QVector3D &p1, QVector3D &p2, QVector3D &p3, bool saveRearFacet, float radius);
    void recomputeAll();

	float minX, minY, minZ;
	float maxX, maxY, maxZ;
	float lastZ;
	int currentLayer;
    int showLayers;
   
    QVector<QVector3D> m_tubeVertices;
    QVector<int> m_tubeIndices;
    QVector<QVector3D> m_tubeNormals;
    QVector<QVector3D> m_prevFacet;

};

#endif /* GCode_H_ */
